package com.coderman.controller.business;

import com.coderman.business.service.IEmpService;
import com.coderman.common.annotation.ControllerEndpoint;
import com.coderman.common.error.SystemException;
import com.coderman.common.model.business.Emp;
import com.coderman.common.response.ResponseBean;
import com.coderman.common.vo.business.EmpVO;
import com.coderman.common.vo.system.PageVO;
import com.wuwenze.poi.ExcelKit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(tags = "员工管理")
@RestController
@RequestMapping("/emp")
public class EmpController {
    @Autowired
    private IEmpService empService;

    @ApiOperation(value = "获取员工列表", notes = "获取员工")
    @GetMapping("/getEmpNameList")
    @ResponseBody
    public ResponseBean<List<Emp>> getEmpNameList() {
        List<Emp> empList= empService.getEmpNameList();
        if(CollectionUtils.isEmpty(empList)){
            return ResponseBean.error(empList);
        }
        return ResponseBean.success(empList);
    }

    /**
     * 员工列表
     *
     * @return
     */
    @ApiOperation(value = "员工列表", notes = "模糊查询员工列表")
    @GetMapping("/findList")
    @ResponseBody
    public ResponseBean<PageVO<Emp>> findUserList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                    @RequestParam(value = "pageSize", defaultValue = "6") Integer pageSize,
                                                    EmpVO empVO) {
        System.out.println("********************:"+empVO);
        PageVO<Emp> empList = empService.findList(pageNum, pageSize, empVO);
        return ResponseBean.success(empList);
    }


    /**
     * 添加员工
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "添加员工失败", operation = "添加员工")
    @RequiresPermissions({"emp:add"})
    @ApiOperation(value = "添加员工")
    @PostMapping("/add")
    @ResponseBody
    public ResponseBean add(@RequestBody @Validated EmpVO empVO) {
        empService.add(empVO);
        return ResponseBean.success();
    }

    /**
     * 编辑员工信息
     *
     * @param id 编号
     * @return
     */
    @ApiOperation(value = "编辑员工")
    @RequiresPermissions({"emp:edit"})
    @GetMapping("/edit/{id}")
    @ResponseBody
    public ResponseBean edit(@PathVariable Integer id) throws SystemException {
        EmpVO empVO = empService.edit(id);
        return ResponseBean.success(empVO);
    }

    /**
     * 更新员工操作
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "更新员工失败", operation = "更新员工")
    @ApiOperation(value = "更新员工")
    @RequiresPermissions({"emp:update"})
    @PutMapping("/update/{id}")
    @ResponseBody
    public ResponseBean update(@PathVariable Integer id, @RequestBody @Validated EmpVO empVO) throws SystemException {
        empService.update(id, empVO);
        return ResponseBean.success();
    }

    /**
     * 删除员工
     *
     * @param id
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "删除员工失败", operation = "删除员工")
    @ApiOperation(value = "删除员工")
    @RequiresPermissions({"emp:delete"})
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public ResponseBean delete(@PathVariable Integer id) throws SystemException {
        empService.delete(id);
        return ResponseBean.success();
    }




    /**
     * 导出excel
     * @param response
     */
    @ApiOperation(value = "导出excel", notes = "导出所有员工的excel表格")
    @PostMapping("/excel")
    @RequiresPermissions("emp:export")
    @ControllerEndpoint(exceptionMessage = "导出Excel失败",operation = "导出员工excel")
    public void export(HttpServletResponse response) {
        List<Emp> emps = this.empService.findAll();
        ExcelKit.$Export(Emp.class, response).downXlsx(emps, false);
    }

    /**
     * 所有员工
     *
     * @return
     */
    @ApiOperation(value = "所有员工")
    @GetMapping("/findAll")
    @ResponseBody
    public ResponseBean<List<EmpVO>> findAll() {
        List<EmpVO> empVOS = empService.findAllVO();
        return ResponseBean.success(empVOS);
    }

}
